<template>
   <div class="product_con">
     <!-- 搜索 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="el-icon-search"></span> <span>筛选搜索</span>
        <el-button
          style="float: right; margin-left: 5px"
          type="primary"
          size="small"
          @click="searchFun"
          >查询</el-button
        >
        <el-button
          style="float: right"
          type="button"
          size="small"
          @click="resetFun"
          >重置</el-button
        >

        
      </div>
      <el-form ref="form" :model="listQuery" label-width="80px" class="tupian">
        <el-form-item label="输入搜索">
          <el-input v-model="listQuery.name" placeholder="商品名称"  style=" width: 150px;
   height: 30px;"></el-input>
        </el-form-item>
        <el-form-item label="商品货号" style="">
          <el-input
          style=" width: 150px;height: 30px;"
            v-model="listQuery.product_sn"
            placeholder="商品货号"
          ></el-input>
        </el-form-item>
             
    <!-- 分类 -->
        <el-form-item label="商品分类">
          <el-cascader
            v-model="productCateValue"
            :options="productCateOptions"
          ></el-cascader>
        </el-form-item>
          
    


            <!-- 分类 -->
        <el-form-item label="上架状态">
          <el-select v-model="listQuery.publish_status" placeholder="全部">
            <el-option
              v-for="item in publishStatusOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品品牌">
          <el-select v-model="listQuery.brand_id" placeholder="全部">
            <el-option
              v-for="item in brandOptions"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      
         <el-form-item label="审核状态">
          <el-select v-model="listQuery.verify_status" placeholder="全部">
            <el-option
              v-for="item in publishshenghe"
              :label="item.lable"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
         
         <el-card class="box-card">
     
    </el-card>
      </el-form>
    </el-card>
      
       <div>
        <span class="el-icon-goods"></span>
        <span> 数据列表</span>
        <el-button
          style="float: right"
          type="button"
          size="small"
          @click="goAddPage"
          >添加</el-button
        >
      </div>
      <!-- 商品列表 -->
     <el-table :data="productList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" class="product_img" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"> </el-table-column>
      <el-table-column  label="价格/货号 ">
        <template slot-scope="scope">
         价格：{{scope.row.price}}  <br>
         货号：{{scope.row.product_sn}}
        </template>
       </el-table-column>
       <el-table-column label="标签"> 
        <template slot-scope="scope">
         上架：{{scope.row.publish_status | publish}}  <br>
         新品：{{scope.row.new_status | publish }} <br>
         推荐: &nbsp; {{scope.row.recommand_status |  publish  }}
        </template>
       </el-table-column>
       <el-table-column prop="sort" label="排序"> </el-table-column>
       <el-table-column prop="sale" label="销量"> </el-table-column>
      <el-table-column label="审核状态">
        <template slot-scope="scope">
          {{ scope.row.verify_status | verifyType }}
        </template>
      </el-table-column>
       <el-table-column  label="操作" > 
           <div class="click" slot-scope="scope">
           <el-button size="small" @click="editFun(scope.row.id)"
            >编辑</el-button
          >  <el-button type="danger" class="click2" @click="delFun(scope.row.id)">删除</el-button>
           </div>
   </el-table-column>
    </el-table>
     
    <!-- 1.拷贝 element ui  分页代码 
    current-page 当前页码
    page-sizes   每页有多少条数据的下拉列表
    page-size    当前的列表每页有多少条数据
    total        数据的总数量
    size-change   修改每页有多少条数据
    current-change 修改当前是第几页
	4.修改了current-page、page-sizes page-size total属性绑定的值
    -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

   </div>
</template>

<script>
//import axios from "axios"
 //引入api接口
 //export 导出时如果没有使用default， 在其他问价引入的时候需要写
 import {  fetchProductList,deleteProduct} from "@/api/product";
 //1.引入mixin
import productmixin from "./productmixin";

export default {
  name: "product",
  data: function () {
    return {
      productList: [],
      //2.定义变量
      listQuery:{
        pageSize:10,//一页10条数据
        pageNum:1,//当前页
        name:"",//商品名称
        product_sn:null,//货号
        publish_status:null,//上架状态
        brand_id:null,//品牌id
      verify_status:null,//审核状态
       product_category_id: null, //分类id
      },
      publishStatusOptions:[
       {value:1 ,lable:"上架"},
       {value:0 ,lable:"下架"},
      ],
      publishshenghe:[
       {value:1 ,lable:"已审核"},
       {value:0 ,lable:"未审核"}
      ],
      productCateOptions: [], //商品分类option的数据
      productCateValue: null, //选中的cate的数据
       brandOptions: [], //商品品牌列表
      total:0
    };
  },
  mounted() {
    //调用获取列表的函数
   this.getProductData();
   //调用获取品牌列表
    this.getBrandData();
        //调用 获取 分类列表
    this.getCateData();
  },
  //过滤器
  filters: {
    verifyType(value) {
      console.log(value);
      return value ? "已审核" : "未审核";
    },
    publish(value){
   return value ? "是" : "否"
    },
 
  },
  watch: {
    //监听级联菜单的选中的数据是否发生变化了
    productCateValue(newV) {
      console.log(newV);
      //找到 数组的1号元素，赋值给 product_category_id
      this.listQuery.product_category_id = newV[1];
    },
  },
  methods: {
      //修改
       //修改
    editFun(id) {
     console.log("修改触发了", id);
      this.$router.push("/pms/productupdate/" + id);
    },
    //删除
    delFun(id) {
       console.log("删除触发了", id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          deleteProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getProductData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 修改每页显示的条数
    handleSizeChange(val) {
      console.log("修改每页显示的条数 触发了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      //重新获取数据
      this.getProductData();
    },
    //6.修改当前页的页码
    handleCurrentChange(val) {
      console.log("修改当前页的页码 触发了", val);
      this.listQuery.pageNum = val;
      this.getProductData();
    },
    //重置
    resetFun() {
      this.listQuery = {
        pageSize: 10, //一页10条数据
        pageNum: 1, //当前页
        name: "", //商品名称
        product_sn: null, //货号
        publish_status: null, //上架状态
        brand_id: null, //品牌id
        product_category_id: null, //分类id
      };
      this.productCateValue = null;
      //重新获取数据
      this.getProductData();
    },
    //跳转到添加页面
    goAddPage() {
      this.$router.push("/pms/productadd");
    },
    

    //***********获取数据********* */
    //获取商品列表
     async getProductData() {
      // fetchProductList().then((res) => {
      //   console.log(res);
      //   this.productList = res.product;
      // });
      var res = await fetchProductList(this.listQuery);
      this.productList = res.product;
      //3..获取数据的总量
      this.total=res.total
    },
   
  
    searchFun() {
      //重新获取数据
      this.getProductData();
    },
  },
   //2.注册mixin
  mixins: [productmixin],
};
</script>

<style lang="scss">
.product_con {
  margin: 20px;
  .product_img {
    width: 100px;
  }
}
.click{
   width: 150px;
   height: 50px;
   .click1{
   width: 50px;
   height: 30px;
 padding: 10px;
   }
   .click2{
   width: 50px;
   height: 30px;
    padding: 10px;
   }
}
 .tupian{
   display: flex;
   flex-wrap: wrap;
 }
</style>